<?php
	echo "
	<script language='javascript'>
		function generateHtmlField(number) {
			content = '<div id=\"field'+number+'\" style=\"border:solid 1px; padding:10px; margin:10px;\">'+
					  '<div class=\"input\">'+
					  '<label for=\"Fcampo_'+number+'Nome\">Nome do campo</label>'+
					  '<input name=\"data[Fcampo_'+number+'][name]\" type=\"text\" value=\"\" id=\"Fcampo_'+number+'Name\" /><br/>'+
					  '</div>'+
					  '<label for=\"Fcampo_'+number+'Type\">Tipo do campo</label>'+
					  '<select name=\"data[Fcampo_'+number+'][type]\" id=\"Fcampo_'+number+'Type\">'+
					  '<option value=\"\">Selecione um tipo</option>'+
					  '<option value=\"string\">Texto</option>'+
					  '<option value=\"texto\">Texto grande</option>'+
					  '<option value=\"inteiro\">Numero</option>'+
					  '<option value=\"data\">Data</option>'+
					  '<option value=\"file\">Arquivo</option>'+
					  '</select><br/>'+
					  '<div class=\"input\">'+
					  '<label for=\"Fcampo_'+number+'Description\">Descricao do campo</label>'+
					  '<input name=\"data[Fcampo_'+number+'][description]\" type=\"text\" value=\"\" id=\"Fcampo_'+number+'Description\" /><br/>'+
					  '</div>'+
					  '<div class=\"input\">'+
					  '<label for=\"Fcampo_'+number+'Help\">Texto de ajuda</label>'+
 					  '<input name=\"data[Fcampo_'+number+'][help]\" type=\"text\" value=\"\" id=\"Fcampo_'+number+'Help\" /><br/>'+
 					  '</div>'+
 					  '<span><a href=\"javascript:void(0);\" onClick=\"removeField('+number+')\">Remover campo</a></span>'+
					  '</div>';
			return content;
		}
	
		function addNewField(number) {
			next = parseInt(number) + 1;
			content = '<div>'+generateHtmlField(number)+'</div><div id=newField'+next+'></div>'
			document.getElementById('newField'+number).innerHTML = content;
			document.getElementById('FormularioFieldIterator').value = next;
		}
		
		function removeField(number) {
			document.getElementById('field'+number).innerHTML = '';
			document.getElementById('field'+number).style.display = 'none';
		}
	</script>
	"; 
	
	echo $accessibility->whereIAm($pageTitle,array(array('title'=>'Formulários','link'=>'/formularios'),array('title'=>'Editar formulário')));

	echo "<h2 id='contentTitle'>".$pageTitle."</h2>";
	if (isset($formulario) && is_array($formulario) && (!empty($formulario))) {
		$qtCampos = count($campos);
		
		echo $form->create();
		echo $form->hidden('id');
		echo $form->input('name',array('label'=>'Nome'));
		echo $form->input('description',array('label'=>'Descrição'));
		// Hidding organizating select
		$unitUser = $othAuth->getData("unit");
		$group = $othAuth->getData('group');
		$level =$group['level'];
		if ($level < 100) {  // system admin
			echo $form->label('organization_id','Entidades');
			echo $form->select('organization_id', $organizations, null, array());
		} else {	
			echo $form->hidden('organization_id', array('value'=>$unitUser['0']['Organization']['id']));
		}
		
		echo "<br />";
		$i = 0;
		$tipo_dados = array(
						'string'=>'Texto',
						'texto'=>'Texto grande',
						'inteiro'=>'Numero',
						'data'=>'Data',
						'file'=>'Arquivo'
					);
		foreach ($campos as $campo) {
			echo $form->hidden('Fcampo_'.$i.'.id', array('value'=>$campo['Fcampo']['id']));
			echo "<div id='field".$i."' style=\"border:solid 1px; padding:10px; margin:10px;\">";
			echo $form->input('Fcampo_'.$i.'.name',array('label'=>'Nome do campo', 'value'=>$campo['Fcampo']['name']));
			echo $form->label('Fcampo_'.$i.'.type','Tipo do campo');
			echo $form->select('Fcampo_'.$i.'.type',$tipo_dados, $campo['Fcampo']['type'], array())."<br/>";
			echo $form->input('Fcampo_'.$i.'.description', array('label'=>'Descricao do campo', 'value'=>$campo['Fcampo']['description']));
			echo $form->input('Fcampo_'.$i.'.help', array('label'=>'Texto de ajuda', 'value'=>$campo['Fcampo']['help']));
			echo $html->link('Remover campo', 'javascript:void(0);' ,array('onClick'=>'removeField(\''.$i.'\');'));
 			
			echo "</div>";
			$i++;
		}
		echo $form->hidden('fieldIterator', array('value'=>$qtCampos));
		echo "<div id='newField".$qtCampos."'></div>";
		echo "<br />"; 
		echo $html->link('Novo campo','javascript:void(0);', array('onClick'=>'addNewField(document.getElementById(\'FormularioFieldIterator\').value);'));
		echo "<br />"; 
		echo "<br />"; 
		echo $form->end('Salvar alterações');
	
	} else {
		echo "<p>Não existe formulario cadastrado com o id fornecido</p>";
	}
